#include "main.h"
#include "scnsInclude.h"

static void readTest(void)
{
    ScnsLcdLayer layN=(SCNS_LCD_LAYER_AUTO);
    ScnsLcdColor *tmp=scnsMallocFrom(SCNS_MALLOC_AXI,sizeof(ScnsLcdColor)*60*60);
    {
        scnsLcdLayerAutoBegin();
        scnsLcdSetAddress(layN,(Point){0,0},(Point){0+60-1,0+60-1},0);
        scnsLcdReadColors(layN,tmp,60*60);
        scnsLcdLayerAutoEnd();
    }
    scnsLcdDisplayArrayImage(SCNS_LCD_LAYER_AUTO,(Point){160,0},tmp,SCNS_LCD_COLOR_FORMAT,NULL,60,60,0,0,0);
    scnsFree(tmp);
}

static uint8 displayImage=0;

static ScnsStatus getImage(ScnsImage*image,uint8*type)
{
    *type=0;
#if defined(SCNS_OV7725_ENABLE)&&SCNS_OV7725_ENABLE==1
    if(SCNS_STATUS_OK==scnsOv7725GetImage(SCNS_OV7725_1,image))
    {
        *type=1;
        return SCNS_STATUS_OK;
    }
#endif
#if defined(SCNS_OV2640_ENABLE)&&SCNS_OV2640_ENABLE==1
    if(SCNS_STATUS_OK==scnsOv2640GetImage(SCNS_OV2640_1,image))
    {
        *type=2;
        return SCNS_STATUS_OK;
    }
#endif
#if defined(SCNS_OV5640_ENABLE)&&SCNS_OV5640_ENABLE==1
    if(SCNS_STATUS_OK==scnsOv5640GetImage(SCNS_OV5640_1,image))
    {
        *type=3;
        return SCNS_STATUS_OK;
    }
#endif

    return SCNS_STATUS_FAILED;
}

static void finishImage(ScnsImage*image,uint8 type)
{
#if defined(SCNS_OV7725_ENABLE)&&SCNS_OV7725_ENABLE==1
    if(type==1){scnsOv7725SetFinish(SCNS_OV7725_1,image);}
#endif
#if defined(SCNS_OV2640_ENABLE)&&SCNS_OV2640_ENABLE==1
    if(type==2){scnsOv2640SetFinish(SCNS_OV2640_1,image);}
#endif
#if defined(SCNS_OV5640_ENABLE)&&SCNS_OV5640_ENABLE==1
    if(type==3){scnsOv5640SetFinish(SCNS_OV5640_1,image);}
#endif
}

void loop_init(void)
{
    displayImage=1;
    scnsMenuAdd(2,displayImage,,SCNS_TYPE_BOOLEAN,1,0,1,1);
}

__attribute__((noinline)) void loop(void)
{
    scnsMenuFresh();
    scnsIwdgFeed();
    if(scnsMenuCanPageDisplay((uint8)-1))
    {
#if defined(SCNS_TOUCH_SCREEN_ENABLE)&&SCNS_TOUCH_SCREEN_ENABLE==1
        scnsTouchScreenDisplayButtonAll(SCNS_LCD_LAYER_AUTO,0);
#endif
        readTest();
    }
    {
        ScnsImage image;
        uint8 type;
        if(SCNS_STATUS_OK==getImage(&image,&type))
        {
            static uint64 lastTick;
            const uint64 tick=scnsTimerGetTick();
            const uint32 fps=scnsTimerGetFpsByTick(tick-lastTick);
            lastTick=tick;
            scnsMenuAddLocalConst(2,fps,,SCNS_TYPE_INT32);
            if(type)
            {
                if(displayImage&&scnsMenuCanPageDisplay(2))
                {
                    scnsLcdDisplayImage(SCNS_LCD_LAYER_AUTO,(Point){.x=0,.y=0},&image,0,0,0);
                }
            }
            if(scnsMenuCanPageSend(2))
            {
                scnsSmctpExtOscilloscopeSendInt32(SCNS_SMCTP_CHANNEL_VISUAL_SCOPE,0,1,fps);
            }
            finishImage(&image,type);
        }
    }
#if defined(SCNS_LCD_ENABLE)&&SCNS_LCD_ENABLE==1&&defined(SCNS_SMCTP_ENABLE)&&SCNS_SMCTP_ENABLE==1&&defined(SCNS_SMCTP_EXT_LOGGER_ENABLE)&&SCNS_SMCTP_EXT_LOGGER_ENABLE==1
    {
        const Point a=scnsLcdGetPhysicalPlu(1);
        const Point b=scnsLcdGetPhysicalPrd(1);
        static uint8 last=0;
        uint8 this=(scnsMenuCanPageDisplay(((uint8)-1))==1);
        if(this){scnsSmctpExtLoggerDisplay(SCNS_LCD_LAYER_AUTO,(Point){b.x-240,a.y},16,0,240,last!=this,6,&scnsLcdFontConsolas10,SCNS_LCD_COLOR_RED,SCNS_LCD_COLOR_BACKGROUND,0);}
        last=this;
    }
#endif
#if defined(SCNS_TM1638_ENABLE)&&SCNS_TM1638_ENABLE==1
    {
        scnsTm1638DisplayPartial(SCNS_TM1638_STB0,(const uint8[6])
            {
                scnsTimerGetMs()/1000000%10,
                scnsTimerGetMs()/100000%10,
                scnsTimerGetMs()/10000%10,
                (scnsTimerGetMs()/1000%10)|0X10,
                scnsTimerGetMs()/100%10,
                scnsTimerGetMs()/10%10,
            },0,6
        );
    }
#endif
}
